<?php

namespace App\Http\Controllers;

use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class informationController extends Controller
{



    //编辑页
    public function info()
    {
        //获取session ID
        $id = session('userid');
        //根据当前 session ID 获取用户昵称 头像 简介
        $res = DB::table('user')
                    ->join('detailed','user.id','=','detailed.d_id')
                    ->where('user.id','=',$id)
                    ->select('user.u_name','user.u_img','user.u_member','user.u_v','detailed.d_biref')->get();


        //var_dump($id,$res);die;

        return view('home/information',['resinfo' => $res]);
    }


    //编辑 分帧页面
    public function ifr()
    {
        //此处要获取seesion 中的用户id
         $id = session('userid');

        //查询数据
//        $res = DB::select('select u.`id`,u.`u_tel`,u.`u_name`,u.`u_regtime`,u.`u_img`,d.`d_name`,d.`d_adres`,d.`d_sex`,d.`d_love`,d.`d_birth`,d.`d_ox`,d.`d_boke`,d.`d_biref`,d.`d_occu`,d.`d_edu`,d.`d_tag`,d.`id`,d.`d_id`,d.`d_real`,d.`d_email`,d.`d_qq`,d.`d_msn`  from  `user` as u,`detailed` as d  where u.`id` = d.`d_id` and d.`d_id`='.$id);

        $res = DB::select('select u.`id`,u.`u_tel`,u.`u_name`,u.`u_regtime`,u.`u_img`,d.`d_name`,d.`d_adres`,d.`d_sex`,d.`d_love`,d.`d_birth`,d.`d_ox`,d.`d_boke`,d.`d_biref`,d.`d_occu`,d.`d_edu`,d.`d_tag`,d.`id`,d.`d_id`,d.`d_real`,d.`d_email`,d.`d_qq`,d.`d_msn`  from  `user` as u,`detailed` as d  where u.`id` = d.`d_id` and d.`d_id`='.$id);

        //var_dump($id,$res);die;

        $result = $res[0];
        //var_dump($result);
        return view('home/ifr/iframe',['res' => $result]);
    }


    //资料编辑

    public function update(Request $request)
    {

        //获取数据
        $name = $request ->d_name;
        $d_id = $request->d_id;


        $arr =array(
        'd_name' => $request ->d_name,
        'd_adres' => $request ->d_adres,
        'd_sex' => $request ->d_sex,
        'd_birth' => $request ->d_dirth,
        'd_ox' => $request->d_ox,
        'd_boke' => $request->d_boke,
        'd_tag'=> $request->d_tag,
        'd_id' => $request->d_id,
        'd_real' => $request->d_real

        );

        //var_dump($arr);

       $res = DB::table('detailed')->where('d_id',$d_id)->update($arr);

        if ($res){

            //执行修改成功   同步修改 user 表中昵称
             $result = DB::table('user')->where('id',$d_id)->update(['u_name' => $name]);

             if ($result){

                  return redirect('ifr');

             }else{

                 return redirect('ifr');

             }

        }else{

            return redirect('ifr');


        }


    }


    //邮箱部分
    public  function email(Request $request)
    {
        //var_dump($request);
        $id = $request->d_id;
        $arr = array(
            'd_email' => $request->d_email,
            'd_qq' => $request->d_qq,
            'd_msn' => $request->d_msn
        );

        //var_dump($arr);die;
        $res = DB::table('detailed')->where('d_id',$id)->update($arr);

        //var_dump($res);
        if ($res){
            return redirect('ifr');
        }else{
            return redirect('ifr');
        }

    }


    //修改密码验证

    public function code($tel)
    {
        include_once ('./demo/SendTemplateSMS.php');

        $a = implode(array_rand(range(0,9),6));

        //获取输入的手机号码
        $tel =  $tel;

        session(['home.yan' => $a]);

        sendTemplateSMS("15151963071",array($a,'2'),"1");

    }


    //验证手机验证码是否正确
    public function yan($code)
    {
         $se = session('home.yan');
         $code = $code;
         if ($code == $se){
             echo '1';

         }else{

             echo '2';
         }
    }
    
    
    //修改密码

    public function mima(Request $request)
    {


       //var_dump($request);
       $id = $request->id;
       $u_pwd = md5($request->u_pwd);

       $arr = array(

           'u_pwd' => md5($request->u_pwd)
       );

       //执行修改语句
        $res = DB::table('user')->where('id',$id)->update($arr);

        if($res){

            return back();
        }else{

            return back();

        }

    }



}
